 {include file="public/user_header" title="通道列表" /}
 <style>
     table tbody tr td:last-child{
         background-color:white;
     }
     .layui-upload-file{
         display: none;
     }
 </style>
  <!-- 依 赖 样 式-->
    <link rel="stylesheet" href="/static/component/layui/css/layui.css" type="text/css"/>
    <link rel="stylesheet" href="/static/index/user/assets/vendor/libs/sweetalert2/sweetalert2.css" />
<!-- Content -->
            <div class="container-xxl flex-grow-1 container-p-y">
             {if condition="(getConfig()['td_notice'] != null || getConfig()['td_notice'] != '')"}
                <div class="col-md-12" style="padding-bottom: 20px;">
                  <div class="card">
                    <div class="card-body">
                    {:getConfig()['td_notice']}
                    </div>
                  </div>
                </div>
                {/if}
              <div class="row">
                <div class="col-md-12">
                
                  <div class="card">
                    <!-- 通 道 列 表 -->
                    <h5 class="card-header border-bottom">
                        <span style="float:left;">通 道 列 表</span> 
                    <button id="add" type="button" style="float:right;" class="btn btn-primary" data-bs-toggle="modal"
                        data-bs-target="#addChannel">
                        <i class="bx bx-plus me-sm-1"></i> <span>新 增</span>
                    </button>
                    </h5>
                    <div class="card-datatable text-nowrap">
                      <table class="datatables-basic table border-top">
                        <thead>
                          <tr>
                            <th>通道ID</th>
                            <th>通道类型</th>
                            <th>通道模式</th>
                            <th>账号标识</th>
                            <th>在线状态</th>
                            <th>收款开关</th>
                            <th>今日收款</th>
                            <th>昨日收款</th>
                            <th>收款总笔数</th>
                            <th>收款总金额</th>
                            <th>创建时间</th>
                            <th>在线时长</th>
                            <th>账号备注</th>
                            <th style="background-color:white;">操作</th>
                          </tr>
                        </thead>
                      </table>
                    </div>
                    <!--/ 通 道 列 表 -->
                    
                    <!--  添 加 通 道 -->
                    <div class="modal fade" id="addChannel" tabindex="-1" aria-hidden="true">
                       <div class="modal-dialog modal-dialog-centered1 modal-simple">
                        <div class="modal-content p-3 p-md-5">
                            <div class="modal-body">
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                <div class="text-center">
                                  <h3 class="mb-2">新 增 通 道</h3>
                                  <p>在此添加你想要添加的通道吧.</p>
                                </div>
                                <form id="channel_form" class="row g-3" onsubmit="return false">
                                    <div class="col-12">
                                      <label class="form-label">通 道 类 型</label>
                                      <select
                                        id="type"
                                        name="type"
                                        class="form-select"
                                      >
                                        <option value="wxpay">微 信</option>
                                        <option value="alipay">支 付 宝</option>
                                        <option value="qqpay">Q Q</option>
                                      </select>
                                    </div>
                                    <div class="col-12">
                                      <label class="form-label">通 道 模 式</label>
                                      <select
                                        lay-filter="code" 
                                        name="code" 
                                        id="code"
                                        class="form-select"
                                      >
                                        {volist name="channel" id="vo"}
                                            <option value="{$vo.code}">{$vo.name}</option>
                                        {/volist}
                                      </select>
                                    </div>
                                    <div class="col-12" id="kaleurl" style="position: relative;">
                                      <label class="form-label">二 维 码 信 息</label>
                                      <input
                                        type="text"
                                        name="qr_url" 
                                        class="form-control"
                                        placeholder="如无法识别请修剪收款码多余边边"
                                      />
                                      <button type="button" class="btn btn-primary file-btn-upload" style="position: absolute;right: 5px;bottom: 0;" >上传图片</button>
                                    </div>
                                    <div class="col-12" id="kalenick">
                                      <label class="form-label">昵称/店铺名</label>
                                      <input
                                        type="text"
                                        name="wxname"
                                        class="form-control"
                                        placeholder="昵称/店铺名"
                                      />
                                    </div>
                                    <div class="col-12" id="qq">
                                      <label class="form-label">QQ号码</label>
                                      <input
                                        type="text"
                                        name="qq"
                                        class="form-control"
                                        placeholder="您的QQ号"
                                      />
                                    </div>
                                    <div class="col-12" id='zfbpid'>
                                      <label class="form-label" id="pid_title">支付宝PID</label>
                                      <input
                                        type="text"
                                        name="zfb_pid" 
                                        id="zfb_pid"
                                        class="form-control"
                                        placeholder="请输入支付宝PID"
                                      />
                                    </div>
                                    <div class="col-12" id='zfbapppid'>
                                      <label class="form-label" id="appid_title">应用APPID</label>
                                      <input
                                        type="text"
                                        name="zfbapppid" 
                                        id="zfbappid"
                                        class="form-control"
                                        placeholder="请输入支付宝应用APPID"
                                      />
                                    </div>
                                    <div class="col-12" id="pkey">
                                      <label class="form-label">支付宝公钥</label>
                                      <textarea class="layui-textarea" name="cookie" placeholder="请输入支付宝公钥"></textarea>
                                    </div>
                                    <div class="col-12" id="akey">
                                      <label class="form-label">应用私钥</label>
                                      <textarea class="layui-textarea" name="aliappkey" placeholder="请输入应用私钥"></textarea>
                                    </div>
                                    <div class="col-12" id="remark">
                                      <label class="form-label" id="remark_title">登录信息</label>
                                      <input
                                        type="text"
                                        name="remark"
                                        class="form-control"
                                        placeholder="请输入登录信息"
                                      />
                                    </div>
                                    <div class="col-12">
                                      <label class="form-label">备注信息</label>
                                      <input
                                        type="text"
                                        name="memo"
                                        class="form-control"
                                        placeholder="备注信息"
                                      />
                                    </div>
                                    <div class="col-12 text-center">
                                      <button type="submit" id="channel_Submit" class="btn btn-primary me-sm-3 me-1">提 交</button>
                                      <button
                                        type="reset"
                                        class="btn btn-label-secondary"
                                      >
                                        重 置
                                      </button>
                                    </div>
                             </form>
                            </div>
                  </div>
                </div>
              </div>
                    <!--/  添 加 通 道 -->
                    
                    <!--  通 道 更 新 -->
                    <div class="modal fade" id="upChannel" tabindex="-1" aria-hidden="true">
                       <div class="modal-dialog modal-dialog-centered1 modal-simple">
                        <div class="modal-content p-3 p-md-5">
                            <div class="modal-body">
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                <div class="text-center">
                                  <h3 class="mb-2">更 新 通 道</h3>
                                  <p>在此更新你的通道吧..</p>
                                </div>
                                <form id="channel_form" class="row g-3" onsubmit="return false">
                                    <div>
                                        <label >当前状态:&nbsp;<span style="color:red" name="status" id="status">等待操作中</span></label>
                                    </div>
                                    <div >
                                        <label>二维码信息:</label>
                                        <div class="text-center">
                                            <img id='src' src="/static/index/images/status/loading.gif" width="150" height="150" alt="请重新获取" />
                                            <div id="wenben"></div>
                                        </div>
                                    </div>
                                    <div>
                                        <label>账号信息:&nbsp;<span style="color:red" name="account" id="account"></span></label>
                                    </div>
                                    <div class="col-12 text-center">
                                      <button type="submit" id="update_Submit" class="btn btn-primary me-sm-3 me-1">确 定</button>
                                      <button
                                        type="reset"
                                        class="btn btn-label-secondary"
                                      >
                                        重 置
                                      </button>
                                    </div>
                             </form>
                            </div>
                  </div>
                </div>
              </div>
                    <!--/  通 道 更 新 -->
                    
                     <!-- 当面付 / 商家账单 / APP自挂 通 道 修 改 -->
                    <div class="modal fade" id="editAliPay" tabindex="-1" aria-hidden="true">
                       <div class="modal-dialog modal-dialog-centered1 modal-simple">
                        <div class="modal-content p-3 p-md-5">
                            <div class="modal-body">
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                <div class="text-center">
                                  <h3 class="mb-2">修 改 通 道</h3>
                                  <p>编辑你的通道信息.</p>
                                </div>
                                <form id="editFrom" class="row g-3" onsubmit="return false">
                                    <input id="editId" name="id" style="display:none;">
                                    <input id="editCode" name="code" style="display:none;">
                                    <div class="col-12" id="editPid" style="display:none;">
                                      <label class="form-label" id="editPid_title">支付宝PID</label>
                                      <input
                                        type="text"
                                        name="pid" 
                                        class="form-control"
                                        placeholder="必须输入支付宝PID"
                                      />
                                    </div>
                                    <div class="col-12" id="editAppId">
                                      <label class="form-label" id="editAppId_title">应用APPID</label>
                                      <input
                                        type="text"
                                        name="appId" 
                                        class="form-control"
                                        placeholder="必须输入支付宝的应用APPID"
                                      />
                                    </div>
                                    <div class="col-12" id="editPublicKey">
                                      <label class="form-label">支付宝公钥</label>
                                      <textarea class="layui-textarea" name="publicKey" placeholder="请输入支付宝公钥"></textarea>
                                    </div>
                                    <div class="col-12" id="editPrivateKey">
                                      <label class="form-label">应用私钥</label>
                                      <textarea class="layui-textarea" name="privateKey" placeholder="请输入支付宝公钥"></textarea>
                                    </div>
                                     <div class="col-12" id="editRemark" style="display:none;">
                                      <label class="form-label" id="editRemark_title">登录信息</label>
                                      <input
                                        type="text"
                                        name="remark"
                                        class="form-control"
                                        placeholder="请输入登录信息"
                                      />
                                    </div>
                                    <div class="col-12">
                                      <label class="form-label">备注信息</label>
                                      <input
                                        type="text"
                                        name="memo"
                                        class="form-control"
                                        placeholder="备注信息"
                                      />
                                    </div>
                                    <div class="col-12 text-center">
                                      <button type="submit" id="editAliPaySubmit" class="btn btn-primary me-sm-3 me-1">提 交</button>
                                      <button
                                        type="reset"
                                        class="btn btn-label-secondary"
                                      >
                                        重 置
                                      </button>
                                    </div>
                             </form>
                            </div>
                  </div>
                </div>
              </div>
                    <!--/  当面付 / 商家账单 / APP自挂 通 道 修 改-->
                    
                     <!-- 微 信 通 道 修 改 -->
                    <div class="modal fade" id="editWxPay" tabindex="-1" aria-hidden="true">
                       <div class="modal-dialog modal-dialog-centered1 modal-simple">
                        <div class="modal-content p-3 p-md-5">
                            <div class="modal-body">
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                <div class="text-center">
                                  <h3 class="mb-2">修 改 通 道</h3>
                                  <p>编辑你的通道信息.</p>
                                </div>
                                <form id="editWxFrom" class="row g-3" onsubmit="return false">
                                    <input id="editWxPayId" name="id" style="display:none;">
                                    <input id="editWxPayCode" name="code" style="display:none;">
                                    <div class="col-12" id="ewm" style="position: relative;">
                                      <label class="form-label">二 维 码 信 息</label>
                                      <input
                                        type="text"
                                        name="qr_url" 
                                        class="form-control"
                                        placeholder="如无法识别请修剪收款码多余边边"
                                      />
                                      <button type="button" class="btn btn-primary file-btn-upload" style="position: absolute;right: 5px;bottom: 0;" >上传图片</button>
                                    </div>
                                    <div class="col-12" id="wxName">
                                      <label class="form-label">昵称/店铺名</label>
                                      <input
                                        type="text"
                                        name="wxname"
                                        class="form-control"
                                        placeholder="昵称/店铺名"
                                      />
                                    </div>
                                    <div class="col-12">
                                      <label class="form-label">备注信息</label>
                                      <input
                                        type="text"
                                        name="memo"
                                        class="form-control"
                                        placeholder="备注信息"
                                      />
                                    </div>
                                    <div class="col-12 text-center">
                                      <button type="submit" id="editWxPaySubmit" class="btn btn-primary me-sm-3 me-1">提 交</button>
                                      <button
                                        type="reset"
                                        class="btn btn-label-secondary"
                                      >
                                        重 置
                                      </button>
                                    </div>
                             </form>
                            </div>
                  </div>
                </div>
              </div>
                    <!--/  微 信 通 道 修 改-->
                    
                    <!-- Q Q 通 道 修 改 -->
                    <div class="modal fade" id="editQQPay" tabindex="-1" aria-hidden="true">
                       <div class="modal-dialog modal-dialog-centered1 modal-simple">
                        <div class="modal-content p-3 p-md-5">
                            <div class="modal-body">
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                <div class="text-center">
                                  <h3 class="mb-2">修 改 通 道</h3>
                                  <p>编辑你的通道信息.</p>
                                </div>
                                <form id="editQQFrom" class="row g-3" onsubmit="return false">
                                    <input id="editQQPayId" name="id" style="display:none;">
                                    <input id="editQQPayCode" name="code" style="display:none;">
                                    <div class="col-12" id="qq">
                                      <label class="form-label">QQ号</label>
                                      <input
                                        type="text"
                                        name="qq"
                                        class="form-control"
                                        placeholder="QQ号"
                                      />
                                    </div>
                                    <div class="col-12">
                                      <label class="form-label">备注信息</label>
                                      <input
                                        type="text"
                                        name="memo"
                                        class="form-control"
                                        placeholder="备注信息"
                                      />
                                    </div>
                                    <div class="col-12 text-center">
                                      <button type="submit" id="editQQPaySubmit" class="btn btn-primary me-sm-3 me-1">提 交</button>
                                      <button
                                        type="reset"
                                        class="btn btn-label-secondary"
                                      >
                                        重 置
                                      </button>
                                    </div>
                             </form>
                            </div>
                  </div>
                </div>
              </div>
                    <!--/  Q Q 通 道 修 改-->
                  </div>
                </div>
              </div>
            </div>
            <!-- / Content -->
            
{include file="public/user_footer" /}
 <!-- / Page Js -->
<script>
    layui.use([ 'form','upload','admin'], function () {
            var $ = layui.jquery;
            var form = layui.form;
            var upload = layui.upload;
            var admin = layui.admin;
            
            
            
            //添加通道响应
            $('#add').click(function(){
                //获取当前第一个通道
                var code =  $('#code').val();
                channel_select(code);
                $('input[type="text"]').val('');
            });
            
            //点击弹窗之外杀死定时器
            $('#upChannel').click(function(e){
                //点击关闭按钮之后替换数据且3秒之后执行清除定时器
                if ($(e.target).closest(".btn-close").length == 1) {
                    $("#status").html("等待操作中");
                    $("#src").attr("src",'/static/index/images/status/loading.gif');
                    setTimeout(function(){
                        for (let i = 0; i < 100000; i++) {
                            clearInterval(i)
                        } 
                    },1000);
    		        
            	}
            	//如果点击非操作界面关闭则清除定时器与替换数据
    	        if ($(e.target).closest(".modal-content").length == 0) {
    		        for (let i = 0; i < 100000; i++) {
                        clearInterval(i)
                    }
                    $("#status").html("等待操作中");
                    $("#src").attr("src",'/static/index/images/status/loading.gif');
            	}
             });

            //通道筛选
            $('#type').change(() => {
				var postdata ={
                    id: $('#type').val()
                }
                $.getJSON("/Channel/type",postdata,function(data){
                    if (data.code) {
                            var list= data.channel;
                            var nr= '';
                            if(list){
                                for(var i = 0; i < list.length; i++) {
                                        nr += "<option value='" + list[i].code + "'>" + list[i].name + "</option>"
                                };
                            }
                            $("#code").html(nr);
                            form.render('select');
                            //获取当前第一个通道
                            var code =  $('#code').val();
                            channel_select(code); 
                    }
                },true);
            });
		    
		    //模式筛选
            $('#code').change(() => {
				//获取当前第一个通道
                var code =  $('#code').val();
                channel_select(code);    
		    });
		    
		    //添加通道
		    $('#channel_Submit').click(function(){
		        let data = {};
		        let value = $('#channel_form').serializeArray();
                $.each(value, function (index, item) {
                    data[item.name] = item.value;
                });
                $.post('/Channel/addchannel', data, function (res) {
                                if (200 == res.code) {
                                    Swal.fire({
                                        icon: 'success',
                                        title: res.msg,
                                        customClass: {
                                          confirmButton: 'btn btn-primary'
                                        }
                                    }).then(function (result) {
                                        if(result.value){
                                            reload();
                                            $(".btn-close").trigger("click");
                                            
                                        }
                                    });
                                    } else {
                                        Swal.fire({
                                            title: res.msg,
                                            icon: 'error',
                                            customClass: {
                                              confirmButton: 'btn btn-primary'
                                            }
                                        });
                                    }
                            }, 'json');
                            return false;
		    });
		    
		    
		    //修改支付宝当面付/商家账单通道
		    $('#editAliPaySubmit').click(function(){
		        let data = {};
		        let value = $('#editFrom').serializeArray();
                $.each(value, function (index, item) {
                    data[item.name] = item.value;
                });
                $.post('/Channel/editAliPay', data, function (res) {
                                if (200 == res.code) {
                                    Swal.fire({
                                        icon: 'success',
                                        title: res.msg,
                                        customClass: {
                                          confirmButton: 'btn btn-primary'
                                        }
                                    }).then(function (result) {
                                        if(result.value){
                                            reload();
                                            $(".btn-close").trigger("click");
                                            
                                        }
                                    });
                                    } else {
                                        Swal.fire({
                                            title: res.msg,
                                            icon: 'error',
                                            customClass: {
                                              confirmButton: 'btn btn-primary'
                                            }
                                        });
                                    }
                            }, 'json');
                            return false;
		    });
		    
		    //修改微信APP挂机/自挂/店员通道
		    $('#editWxPaySubmit').click(function(){
		        let data = {};
		        let value = $('#editWxFrom').serializeArray();
                $.each(value, function (index, item) {
                    data[item.name] = item.value;
                });
                $.post('/Channel/editWxPay', data, function (res) {
                                if (200 == res.code) {
                                    Swal.fire({
                                        icon: 'success',
                                        title: res.msg,
                                        customClass: {
                                          confirmButton: 'btn btn-primary'
                                        }
                                    }).then(function (result) {
                                        if(result.value){
                                            reload();
                                            $(".btn-close").trigger("click");
                                            
                                        }
                                    });
                                    } else {
                                        Swal.fire({
                                            title: res.msg,
                                            icon: 'error',
                                            customClass: {
                                              confirmButton: 'btn btn-primary'
                                            }
                                        });
                                    }
                            }, 'json');
                            return false;
		    });
		    
		    //修改QQ通道
		    $('#editQQPaySubmit').click(function(){
		        let data = {};
		        let value = $('#editQQFrom').serializeArray();
                $.each(value, function (index, item) {
                    data[item.name] = item.value;
                });
                $.post('/Channel/editQQPay', data, function (res) {
                                if (200 == res.code) {
                                    Swal.fire({
                                        icon: 'success',
                                        title: res.msg,
                                        customClass: {
                                          confirmButton: 'btn btn-primary'
                                        }
                                    }).then(function (result) {
                                        if(result.value){
                                            reload();
                                            $(".btn-close").trigger("click");
                                            
                                        }
                                    });
                                    } else {
                                        Swal.fire({
                                            title: res.msg,
                                            icon: 'error',
                                            customClass: {
                                              confirmButton: 'btn btn-primary'
                                            }
                                        });
                                    }
                            }, 'json');
                            return false;
		    });
	
            //通道更新
            $('#update_Submit').click(function(){
                var status = $('#status').text();
                if(status == '账号登录成功!'){
                    Swal.fire({
                        icon: 'success',
                        title: '更新成功',
                        customClass: {
                          confirmButton: 'btn btn-primary'
                        }
                     }).then(function (result) {
                         if(result.value){
                             reload();
                             $("#status").html('等待操作中');
                             $(".btn-close").trigger("click");
                         }
                     });
                }else{
                    Swal.fire({
                        title: '账户未更新',
                        icon: 'error',
                        customClass: {
                          confirmButton: 'btn btn-primary'
                        }
                    });
                }
                
            });
            
            //上传图片
            var uploadInst = upload.render({
                            elem: '.file-btn-upload'
                            ,url: '/channel/upload'
                            ,size: {:getConfig()['imageSize']} //限制文件大小，单位 KB
                            //,accept: 'file'
                            ,method: 'get'
                            ,fileAccept: 'image/*'
                            ,exts: "jpg|png|jpeg"
                            ,data: { //额外参数
                              a: 1
                              ,channel_code: function(){
                                    var code =  $('#code').val();
                                    if(code == null || code == '' || code == 'wxpay_cloud'){
                                        code =  $('#editWxPayCode').val();
                                    }
                                    return code;
                              }
                              ,b: function(){
                                return 2
                              }
                            }
                            ,done: function(res, index){
                            
                              //如果上传失败
                              if(res.code > 0){
                                //上传失败
                                Swal.fire({
                                        icon: 'error',
                                        title: res.msg,
                                        customClass: {
                                          confirmButton: 'btn btn-primary'
                                        }
                                    })
                              }
                              $('input[name = "qr_url"]').val(res.data.src);
                              
                              //上传成功
                               Swal.fire({
                                    icon: 'success',
                                    title: res.msg,
                                    customClass: {
                                      confirmButton: 'btn btn-primary'
                                    }
                                })
                            }
                            ,error: function(index, upload){
                                console.log(1111);
                                return false;
                              this.item.html('重选上传');
                              
                              //演示失败状态，并实现重传
                              var demoText = $('#demoText');
                              demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                              demoText.find('.demo-reload').on('click', function(){
                                uploadInst.upload();
                              });
                              
                              element.progress('demo', '0%');
                            }
                            ,progress: function(n, elem, res, index){
                              console.log(n + '%', elem, res, index); //获取进度百分比
                              element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                            }
                          });
    });
    var dt_basic_table = $('.datatables-basic');
     // DataTable
            // --------------------------------------------------------------------
    if (dt_basic_table.length) {
             var table =  dt_basic_table.DataTable({
                 ajax: '/Channel/Index',
                 columns: [
                   { data: 'id' },
                   { data: 'type' },
                   { data: 'code_name' },
                   { data: 'zfb_pid' },
                   { data: 'status' },
                   { data: 'is_status' },
                   { data: 'today_money'},
                   { data: 'yesterday_money' },
                   { data: 'succcount'},
                   { data: 'succprice' },
                   { data: 'create_time' },
                   { data: 'online_time' },
                   { data: 'memo' },
                   { data: 'id' },
                 ],
                 columnDefs: [
                    {
                      // Label
                      targets: 1,
                      render: function (data, type, full, meta) {
                        var $type = full['type'];
                        var $array = {
                          wxpay: { title: '微信', class: 'bg-label-success' },
                          alipay: { title: '支付宝', class: ' bg-label-info' },
                          qqpay: { title: 'QQ', class: ' bg-label-danger' },
                        };
                        if (typeof $array[$type] === 'undefined') {
                          return data;
                        }
                        return (
                          '<span class="badge rounded-pill ' +
                          $array[$type].class +
                          '">' +
                          $array[$type].title +
                          '</span>'
                        );
                      }
                    },
                    {
                      // Label
                      targets: 2,
                      render: function (data, type, full, meta) {
                        var $type = full['code'];
                        var $code_name = full['code_name'];
                        var $cloud_name = full['cloud_name'];
                        
                        var $array = {
                          wxpay_cloud: { title: $code_name , cloud: $cloud_name ,class: 'bg-label-success' },
                          wxpay_cloudzs: { title: $code_name , cloud: $cloud_name , class: ' bg-label-success' },
                          wxpay_skd: {title: $code_name , cloud: $cloud_name , class: 'bg-label-success' },
                          qqpay_cloud: { title: $code_name , cloud: $cloud_name , class: ' bg-label-danger' },
                          qqpay_wzq: { title: $code_name , cloud: $cloud_name , class: ' bg-label-danger' },
                        };
                        if (typeof $array[$type] === 'undefined') {
                          return data;
                        }
                        return (
                          $array[$type].title + 
                          '&nbsp;-&nbsp;<span class="badge rounded-pill ' +
                          $array[$type].class +
                          '">' +
                          $array[$type].cloud + 
                          '</span>'
                        );
                      }
                    },
                    {
                      // Label
                      targets: 3,
                      render: function (data, type, full, meta) {
                        var $code = full['code'];
                        var $type = full['type'];
                        if($type == 'wxpay'){
                            if($code == 'wxpay_dy'){
                                return  full['wxname'];
                            }else if($code == 'qqpay_wzq'){
                                return full['qq'];
                            }
                            return full['wx_guid'];
                        }else if($type == 'alipay'){
                            if($code == 'alipay_dmf'){
                                return full['wxname'];
                            }else{
                                return full['zfb_pid'];
                            }
                        }else if($type == 'qqpay'){
                            return full['qq'];
                        }
                      }
                    },
                    {
                      // Label
                      targets: 4,
                      render: function (data, type, full, meta) {
                        var $status = full['status'];
                        var $array = {
                            0: { title: '离线', class: 'bg-label-danger' },
                            1: { title: '在线', class: 'bg-label-success' },
                        };
                        return (
                          '<span class="badge rounded-pill ' +
                          $array[$status].class +
                          '">' +
                          $array[$status].title +
                          '</span>'
                        );
                      }
                    },
                    {
                      // Label
                      targets: 5,
                      render: function (data, type, full, meta) {
                        var is_status = full['is_status'];
                        var id = full['id'];
                        var checked = '';
                        if(is_status == 1){
                            checked = 'checked';
                        }
                        return (
                          '<label class="switch">'+
                            '<input type="checkbox" id="up_status_'+ id +'" name="'+ id +'" class="switch-input" '+ checked +'/>'+
                            '<span class="switch-toggle-slider">'+
                              '<span class="switch-on">'+
                                '<i class="bx bx-check"></i>'+
                              '</span>'+
                              '<span class="switch-off">'+
                               '<i class="bx bx-x"></i>'+
                              '</span>'+
                            '</span>'+
                          '</label>'
                        );
                      }
                    },
                    {
                      // Label
                      targets: -1,
                      searchable: false,
                      orderable: false,
                      render: function (data, type, full, meta) {
                        var id = full['id'];
                        var code = full['code'];
                        var type = full['type'];
                        var arr = [id,"'" + type + "'","'" + code + "'","'" + full['qq'] + "'"];
                        if(type == 'wxpay'){
                            var editArr = [id,"'" + code + "'","'" + full['wxname'] + "'","'" + full['qr_url'] + "'","'" + full['memo'] + "'"];
                        }else if(type == 'alipay'){
                            var editArr = [id,"'" + code + "'","'" + full['zfb_pid'] + "'","'" + full['wxname'] + "'","'" + full['cookie'] + "'","'" + full['qr_url'] + "'","'" + full['remark'] + "'","'" + full['memo'] + "'"];
                        }else if(type == 'qqpay'){
                            var editArr = [id,"'" + code + "'","'" + full['qq'] + "'","'" + full['memo'] + "'"];
                        }else{
                            var editArr = [id,"'" + code + "'","'" + full['wxname'] + "'","'" + full['qr_url'] + "'","'" + full['memo'] + "'"];
                        }
                        
                        var del = '<button type="button" onclick="del('+id+')" class="btn rounded-pill btn-sm btn-google-plus">删除</button>';
                        if(code == "alipay_pc" ){
                            return (
                                del
                            ); 
                        }else if(code == "wxpay_dy" ){
                            return (
                                '<button type="button" data-bs-toggle="modal" data-bs-target="#editWxPay" onclick="editWxPay('+editArr+')" class="btn rounded-pill btn-sm btn-info">修改</button>&nbsp;'+
                                '<button type="button" data-bs-toggle="modal" data-bs-target="#upChannel" onclick="update('+arr+')" class="btn rounded-pill btn-sm btn-secondary">绑定</button>&nbsp;'+
                                del
                            ); 
                        }else if(code == "wxpay_app" || code == "wxpay_zg"){
                            return (
                                '<button type="button" data-bs-toggle="modal" data-bs-target="#editWxPay" onclick="editWxPay('+editArr+')" class="btn rounded-pill btn-sm btn-info">修改</button>&nbsp;'+
                                del
                            ); 
                        }else if(code == "alipay_dmf" || code == "alipay_app"){
                            return (
                                
                                '<button type="button" data-bs-toggle="modal" data-bs-target="#editAliPay" onclick="editAliPay('+editArr+')" class="btn rounded-pill btn-sm btn-info">修改</button>&nbsp;'+
                                del
                            );
                        }else if(code == "qqpay_zg"){
                            return (
                                '<button type="button" data-bs-toggle="modal" data-bs-target="#editQQPay" onclick="editQQPay('+editArr+')" class="btn rounded-pill btn-sm btn-info">修改</button>&nbsp;'+
                                del
                            );
                        }else{
                           return (
                                '<button type="button" data-bs-toggle="modal" data-bs-target="#upChannel" onclick="update('+arr+')" class="btn rounded-pill btn-sm btn-twitter">更新</button>&nbsp;'+
                                del
                            ); 
                        }
                        
                      }
                    },
                ],
                 order: [[0, 'desc']],
                 dom: '<"row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6 d-flex justify-content-center justify-content-md-end"f>>t<"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>',
                 displayLength: 10,
                 lengthMenu: [10, 20, 50],
                 scrollX:        true,
                 deferRender: true,
                 fixedColumns:   {
                     left: 0,
                     right: 1
                 }
            });
            }
    //刷新表单
    function reload(){
        table.ajax.reload();
        // window.location.reload();
    }
    //模式筛选
    function channel_select(code){
        $("#kaleurl").hide();
        $("#kalenick").hide();
        $("#qq").hide();
        $("#zfbpid").hide();
        $("#akey").hide();
        $("#pkey").hide();
        $("#remark").hide();
        //zfbapppid
        $("#zfbapppid").hide();
        $("#pid_title").html('支付宝PID');
        $("#appid_title").html('应用APPID');
        $("#zfb_pid").attr('placeholder','请输入支付宝PID');
        $("#zfbappid").attr('placeholder','请输入支付宝应用APPID');
        $("#pkey").html('<label class="form-label">支付宝公钥</label><textarea class="layui-textarea" name="cookie" placeholder="请输入支付宝公钥"></textarea>');
        $("#akey").html('<label class="form-label">应用私钥</label><textarea class="layui-textarea" name="aliappkey" placeholder="请输入应用私钥"></textarea>');
        $("#remark_title").html('登录信息');
        $('input[name="remark"]').attr("placeholder",'请输入登录信息');
        if(code == 'wxpay_dy'){
            $("#kaleurl").show();
            $("#kalenick").show(); 
        }else if(code == 'wxpay_app' || code == 'wxpay_zg'){
            $("#kaleurl").show();
        }else if(code == 'alipay_app'){
            $("#zfbpid").show();
        }else if(code == 'alipay_dmf'){
            $("#zfbapppid").show();
            $("#akey").show();
            $("#pkey").show();
        }
        else if(code == 'alipay_mck'){
            $("#zfbpid").show();
            $("#akey").show();
            $("#pkey").show();
            $("#zfbapppid").show();
        }else if(code == 'qqpay_mg' || code == 'qqpay_zg'){
            $("#qq").show();
        }
    }
    
    //更改收款状态
    $(document).on('click', 'input[type="checkbox"][id^="up_status"]', function() {
      const id = $(this).attr("name");
      const status = $(this).is(':checked') ? 1 : 0;
    
      $.post("/Channel/SaveStatus", { id, status }, function (res) {
        if (res.code == 1) {
          handleSuccess(res.msg, reload);
        } else {
          handleError(res.msg);
        }
      }, 'json');
    });

    function handleSuccess(msg, callback) {
      Swal.fire({
        icon: 'success',
        title: msg,
        customClass: {
          confirmButton: 'btn btn-primary'
        }
      }).then(function (result) {
        if (result.value && typeof callback === 'function') {
          callback();
        }
      });
    }

    function handleError(msg) {
      Swal.fire({
        title: msg,
        icon: 'error',
        customClass: {
          confirmButton: 'btn btn-primary'
        }
      });
    }
    
    //更新通道
    function update(id,type,code,qq) {
            var t1;
            $("#src").attr("src","/static/index/images/status/loading.gif");
            if (code == "wxpay_dy")
            {
                var url = "{:getConfig()['diy_clerkqr']}";
                if (url == null || url == "")
                {
                    url = "/static/index/images/status/loading.gif";
                }
                $("#status").html("店员绑定中...");
                $("#src").attr("src",url);
                $("#account").html("店员监控请联系站长添加好友以及店员");
                return false;
            }
                        
                        
            $.post('/Channel/GetQrlistQrcode', { id: id }, function (updata) {
                            
                            if (updata.code === 1) {
                                function LoginStatus(id) {
                                    $.ajax({
                                        type:'post',
                                        url:'/Channel/GetChannelLoginStatus',
                                        data:{id:id},
                                        dataType:'json',
                                        success:function(res){
                                            if (res.code == 1)
                                            {
                                                clearInterval(t1);
                                                $("#status").html(res.msg);
                                                $("#account").html(res.nick);
                                                $("#src").attr("src", "/static/index/images/status/pay_ok.png");
                                            }else if(res.code == 404){
                                                clearInterval(t1);
                                                $("#status").html(res.msg);
                                                $("#account").html(res.nick);
                                                $("#src").attr("src", "/static/index/images/status/qrcode_timeout.png");
                                            }
                                            else
                                            {
                                                $("#status").html(res.msg);
                                            }
                                        }
                                    });
                                }
                                //获取成功

                                if (type == "wxpay")
                                {
                                    if(updata.qr_url == null || updata.qr_url == ''){
                                       var url = '/static/index/images/status/loading.gif';
                                    }else{
                                        var url = "data:image/png;base64," + updata.qr_url;
                                    }
                                    
                                    $("#src").attr("src", url);
                                    $("#account").html('请使用手机对着手机扫或电脑配合手机扫码，不可使用截图扫码');
                                    //获取后启动定时器
                                    t1 = setInterval(function () { LoginStatus(id) }, 3000);
                                    return false;
                                }
                                if (type == "alipay") {
                                    var url =updata.qr_url;
                                    $("#src").attr("src", url);
                                    $("#account").html('请使用手机对着手机扫或电脑配合手机扫码，不可使用截图扫码');
                                    //获取后启动定时器
                                    t1 = setInterval(function () { LoginStatus(id) }, 3000);
                                    return false;
                                }
                                if (type == "qqpay") {
                                    if(updata.qr_url == null || updata.qr_url == ''){
                                       var url = '/static/index/images/status/loading.gif';
                                    }else{
                                        var url = "data:image/png;base64," + updata.qr_url;
                                    }
                                    $("#src").attr("src", url);
                                    $("#account").html('请使用手机对着手机扫或电脑配合手机扫码，不可使用截图扫码');
                                    //获取后启动定时器
                                    t1 = setInterval(function () { LoginStatus(id) }, 3000);
                                    return false;
                                }


                            } else {
                                $("#src").attr("src", "/static/index/images/status/loading.gif");
                                $("#account").html(updata.msg);
                                notice.msg(updata.msg, { icon: 2 });
                            }
                        }, 'json');
        }
    
    //更新QQ通道内容 通道ID,通道类型,微信名称,解码信息,备注
    function editQQPay(id,code,qq,memo){
        if(code == 'qqpay_zg'){
            $('#editQQPayId').val(id);
            $('#editQQPayCode').val(code);
            $('input[name="qq"]').val(qq);
            $('input[name="memo"]').val(memo);
        }
    }
    
    //更新微信通道内容 通道ID,通道类型,微信名称,解码信息,备注
    function editWxPay(id,code,wxName,qrUrl,memo){
        if(code == 'wxpay_dy'){
            $('#wxName').show();
            $('#editWxPayId').val(id);
            $('#editWxPayCode').val(code);
            $('input[name="qr_url"]').val(qrUrl);
            $('input[name="wxname"]').val(wxName);
            $('input[name="memo"]').val(memo);
        }else{
            $('#wxName').hide();
            $('#editWxPayId').val(id);
            $('#editWxPayCode').val(code);
            $('input[name="qr_url"]').val(qrUrl);
            $('input[name="memo"]').val(memo);
        }
    }
    
    //更新支付宝当面付/APP/ 通道ID,通道类型,PID,APPID,PublicKey,PrivateKey,登录信息,备注
    function editAliPay(id,code,pid,appID,PublicKey,PrivateKey,remark,memo){

        $('#editRemark').hide();
        $("#editPid_title").html('支付宝PID');
        $("#editAppId_title").html('应用APPID');
        $('input[name="pid"]').attr('placeholder','请输入支付宝PID');
        $('input[name="appId').attr('placeholder','请输入支付宝应用APPID');
        $("#editPublicKey").html('<label class="form-label">支付宝公钥</label><textarea class="layui-textarea" name="publicKey" placeholder="请输入支付宝公钥"></textarea>');
        $("#editPrivateKey").html('<label class="form-label">应用私钥</label><textarea class="layui-textarea" name="privateKey" placeholder="请输入应用私钥"></textarea>');
        if(code == 'alipay_mck'){
            $('#editPid').show();
            $('#editAppId').show();
            $('#editPublicKey').show();
            $('#editPrivateKey').show();
            $('#editId').val(id);
            $('#editCode').val(code);
            $('input[name="pid"]').val(pid);
            $('input[name="appId"]').val(appID);
            $('textarea[name="publicKey"]').val(PublicKey);
            $('textarea[name="privateKey"]').val(PrivateKey);
            $('input[name="memo"]').val(memo);
        }else if(code == 'alipay_app'){
            $('#editPid').show();
            $('#editAppId').hide();
            $('#editPublicKey').hide();
            $('#editPrivateKey').hide();
            $('#editId').val(id);
            $('#editCode').val(code);
            $('input[name="pid"]').val(pid);
        }else{
            $('#editPid').hide();
            $('#editAppId').show();
            $('#editPublicKey').show();
            $('#editPrivateKey').show();
            $('#editId').val(id);
            $('#editCode').val(code);
            $('input[name="appId"]').val(appID);
            $('textarea[name="publicKey"]').val(PublicKey);
            $('textarea[name="privateKey"]').val(PrivateKey);
            $('input[name="memo"]').val(memo);
        }
    }

    
    //删除通道
    function del(id) {
        Swal.fire({
            text: '确定要删除吗?删除账号,账号相关的订单也会删除,可能造成您无法核对订单的情况,已知晓请确认删除!',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: '确 定',
            cancelButtonText:'取消',
            customClass: {
              confirmButton: 'btn btn-primary me-2',
              cancelButton: 'btn btn-label-secondary'
            },
            buttonsStyling: false
          }).then(function (result) {
            if (result.value) {
              $.get("/Channel/DelChannel", {
                id: id
            }, function (res) {
                
                if (res.code == 1) {
                    Swal.fire({
                        icon: 'success',
                        title: res.msg,
                        customClass: {
                          confirmButton: 'btn btn-primary'
                        }
                     }).then(function (result) {
                         if(result.value){
                             location.reload();
                         }
                     });
                } else {
                    Swal.fire({
                        title: res.msg,
                        icon: 'error',
                        customClass: {
                          confirmButton: 'btn btn-primary'
                        }
                    });
                }
            }, 'json');
            }
          });
            }
</script>